<?php
require_once("../data/common.php");

if ( !$conn = mysql_connect($mysql_host,$mysql_user,$mysql_pass) )
        print "Unable to connect to the database.";
else
if (!mysql_select_db($mysql_db, $conn)) print mysql_errno($conn) . ": " . mysql_error($conn);


if (isset($_GET['track']) AND isset($_GET['device']) AND isset($_GET['type'])) {
	if ($_GET["track"] && $_GET["device"] && $_GET["type"] == "kml") {
	
		$hours = $_GET["hours"] ? $_GET["hours"] : 12;
		$query = 'SELECT * FROM gps_data LEFT JOIN (users, users_devices) ON (gps_data.device_id=users_devices.id AND users_devices.user_id=users.id) WHERE device_id = "'.mysql_real_escape_string($_GET["device"]).'" AND time_received > DATE_ADD(UTC_TIMESTAMP(), INTERVAL -'.mysql_real_escape_string($hours).' HOUR) ORDER BY time_received';
		//var_dump($query);
		$rs = mysql_query($query);
	
		if (isset($_GET["harvennus"])) {
			$harvennus = $_GET["harvennus"] ? $_GET["harvennus"] : 2;
		} else {
			$harvennus = 0;
		}
		$max_pisteet = 600;
		$gps_locations = mysql_num_rows($rs);
		$rowcount = 0;
		if (count($gps_locations) > $max_pisteet) { $harvennus = ceil(count($gps_locations)/$max_pisteet); }
	
		$lastlat = 0;
		$lastlon = 0;
		$full_distance = 0;
		$poo = 0;
	Header("Content-type: application/vnd.google-earth.kml+xml");
	print '<?xml version="1.0" encoding="UTF-8"?>
	';
	
	print '<kml xmlns="http://earth.google.com/kml/2.0">
	<Document>
	<Placemark>
	<description>
	<![CDATA[
	<table>
	<tr><td>Device</td><td>'.$_GET["device"].'</td></tr>
	</table>
	]]>
	</description>
	<name>test</name>
	<LookAt>
	<longitude>25.005510</longitude>
	<latitude>60.240042</latitude>
	</LookAt>
	<visibility>1</visibility>
	<open>0</open>
	<Style>
	<LineStyle><color>ff0000ff</color><width>3</width></LineStyle>
	<PolyStyle><color>7f00ff00</color></PolyStyle>
	</Style>
	<LineString><extrude>1</extrude><altitudeMode>clampToGround</altitudeMode>
	<coordinates>';
	
		while ($u = mysql_fetch_array($rs)) {
	
			if ($harvennus == 0) {
				if ($poo == 0)
					$distance = 0;
				else
					$distance = distance($lastlat,$lastlon,$u["lat"],$u["lon"]);
				$full_distance = $full_distance + $distance;
	
				printf('%s,%s,%s'."\n",
					$u["lon"],
					$u["lat"],
					round($u["alt"]*0.3048,0)
				);
				$lastlat = $u["lat"];
				$lastlon = $u["lon"];
				$poo = 1;
				$rowcount++;
			} else {
				if ($rowcount % $harvennus == 1 ) {
					if ($poo == 0)
						$distance = 0;
					else
						$distance = distance($lastlat,$lastlon,$u["lat"],$u["lon"]);
					$full_distance = $full_distance + $distance;
		
					printf('%s,%s,%s'."\n",
						$u["lon"],
						$u["lat"],
						round($u["alt"]*0.3048,0)
					);
					$lastlat = $u["lat"];
					$lastlon = $u["lon"];
					$poo = 1;
				}
				$rowcount++;				
			}
		}
	print '</coordinates>
	</LineString>
	</Placemark>
	
	</Document>
	</kml>';
	
	exit;
	}
}

Header("Content-type: text/xml");
# Header("Content-type: text/plain");
print '<?xml version="1.0"?>
<data>';

if (isset($_GET["track"]) AND isset($_GET["device"])) {
	if ($_GET["track"] && $_GET["device"]) {
		$hours = $_GET["hours"] ? $_GET["hours"] : 12;
		$query = 'SELECT * FROM gps_data LEFT JOIN (users, users_devices) ON (gps_data.device_id=users_devices.id AND users_devices.user_id=users.id) WHERE device_id = "'.mysql_real_escape_string($_GET["device"]).'" AND time_received > DATE_ADD(UTC_TIMESTAMP(), INTERVAL -'.mysql_real_escape_string($hours).' HOUR) ORDER BY time_received';
		$rs = mysql_query($query);

		if (isset($_GET["harvennus"])) {
			$harvennus = $_GET["harvennus"] ? $_GET["harvennus"] : 2;
		} else {
			$harvennus = 0;
		}
		$max_pisteet = 600;
		$gps_locations = mysql_num_rows($rs);
		$rowcount = 0;
		if (count($gps_locations) > $max_pisteet) { $harvennus = ceil(count($gps_locations)/$max_pisteet); }

		$lastlat = 0;
		$lastlon = 0;
		$full_distance = 0;
		$poo = 0;
		while ($u = mysql_fetch_array($rs)) {

			if ($harvennus == 0) {
				if ($poo == 0)
				$distance = 0;
				else
				$distance = distance($lastlat,$lastlon,$u["lat"],$u["lon"]);
				$full_distance = $full_distance + $distance;

				printf('<point lat="%s" lon="%s" time_received="%s" alt="%s" speed="%s" course="%s" hdop="%s" distance_to_last="%s" distance="%s" />'."\n",
				$u["lat"],
				$u["lon"],
				$u["time_received"],
				round($u["alt"]*0.3048,0),
				round($u["speed"]*1.852,1),
				$u["course"],
				$u["hdop"],
				$distance,
				$full_distance
				);
				$lastlat = $u["lat"];
				$lastlon = $u["lon"];
				$poo = 1;
			} else {
				if ( $rowcount % $harvennus == 1 ) {
					if ($poo == 0)
					$distance = 0;
					else
					$distance = distance($lastlat,$lastlon,$u["lat"],$u["lon"]);
					$full_distance = $full_distance + $distance;

					printf('<point lat="%s" lon="%s" time_received="%s" alt="%s" speed="%s" course="%s" hdop="%s" distance_to_last="%s" distance="%s" />'."\n",
					$u["lat"],
					$u["lon"],
					$u["time_received"],
					round($u["alt"]*0.3048,0),
					round($u["speed"]*1.852,1),
					$u["course"],
					$u["hdop"],
					$distance,
					$full_distance
					);
					$lastlat = $u["lat"];
					$lastlon = $u["lon"];
					$poo = 1;
				}
			}
			$rowcount++;
		}
	}
}

print "</data>\n";

?>
